//
//  LDMyResumeViewControllerInOne_Show.swift
//  DingDongZP
//
//  Created by LeMac on 16/12/30.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

@objc
class LDMyResumeViewControllerInOne_Show: LDBaseCustomizeVC {
    
    
    var headview:myResumeHeadView?
    var BtnH:CGFloat = IPHONE6UP ? 60 : 45
    var tableView:UITableView?
    
    lazy var shareModel = DDShareModel()
    //点击语音的按钮
    var tapedVoiceBtn:baseVoiceButton? {
        
        didSet{
        
            if audioPlayer.isPlaying {
                audioPlayer.pause()
            }
        }
    }
    
    lazy var btnGampView:UIView = {
        
        let gamp = UIView()
        gamp.frame = CGRectMake(0, LDFrame.ScreenHeight - self.BtnH, LDFrame.ScreenWidth, self.BtnH)
        return gamp
    }()
    
    //播放器
    lazy var audioPlayer:LDAudioPlayer = {
        let play = LDAudioPlayer.sharePlayer()
        
        
//        if play.isPlaying == true {
//            
//            play.pause()
//            
//        }
        
        play.playProgress = {[weak self]
            (obj,pro) in
            self?.tapedVoiceBtn?.animaPlay()
        }
        
        play.playEnd = {[weak self] obj in
            
            self?.tapedVoiceBtn?.animaStop()
        }
        
        play.playNew = {[weak self] obj in
            
            self?.tapedVoiceBtn?.animaStop()
        }
        
        return play
    }()
    
    lazy var shareView:DDShareView = {
        
        let lazyview:DDShareView = NSBundle.mainBundle()
            .loadNibNamed("DDShareView", owner: self, options: nil).first as? DDShareView ?? DDShareView()
        lazyview.userInteractionEnabled = true
        
        return lazyview
    }()
    
    lazy var headView:UIView = {
        
        let headV = UIView()
        headV.backgroundColor = LDGolbalColor.GreenAssist
        let titleLab = UILabel.setDetailsLab("叮咚快聘 • 电子商务招聘就是快", fontSize: 15, titleColor: LDGolbalColor.White, Alignment: .Center)
        headV.addSubview(titleLab)
        
        titleLab.snp_makeConstraints(closure: { (make) in
            
            make.center.equalTo(headV.snp_center)
            make.height.equalTo(25)
        })
        
        return headV
    }()
    
    private var cellList = [LDMyResumeBaseCellInAll]()
    private var cellHeightList = [CGFloat]()
    
    dynamic var model:DDMyResumeModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initData()
        setupView()
        initKVO()
    }
    
    func setupView() {
        
        title = "简历预览"
        audioPlayer.delegate = self
        
        tableView = LDBaseTableView.init(frame: LDFrame.ScreenBounds, style: UITableViewStyle.Plain)
        tableView?.delegate = self
        tableView?.dataSource = self
        
        
        tableView?.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(tableView!)
        //        headview = myResumeHeadView.init(frame: CGRectMake(0, 0, LDFrame.ScreenWidth, 80))
        //        tableView?.tableHeaderView = headview
        
        headView.frame = CGRectMake(0, 0, LDFrame.ScreenWidth, 44)
        tableView?.tableHeaderView = headView
        
        
        let cell0 = LDMyResumeCellAvatarInAll(style: .Default, reuseIdentifier: NSStringFromClass(LDMyResumeCellAvatarInAll.self))
        let cell1 = LDMyResumeCellIPersonInfoInAll(style: .Default, reuseIdentifier: NSStringFromClass(LDMyResumeCellIPersonInfoInAll.self))
        let cell2 = LDMyResumeCellExpectJobInAll(style: .Default, reuseIdentifier: NSStringFromClass(LDMyResumeCellExpectJobInAll.self))
        let cell3 = LDMyResumeCellMyAdvantageInAll(style: .Default, reuseIdentifier: NSStringFromClass(LDMyResumeCellMyAdvantageInAll.self))
        let cell4 = LDMyResumeCellExperienceInAll(style: .Default, reuseIdentifier: NSStringFromClass(LDMyResumeCellExperienceInAll.self))
        let cell5 = LDMyResumeCellEduInAll(style: .Default, reuseIdentifier: NSStringFromClass(LDMyResumeCellEduInAll.self))
        
        cellList = [cell0,cell1,cell2,cell3,cell4,cell5]
        tableView?.snp_makeConstraints(closure: { (make) in
            
            make.left.right.top.equalTo(0)
            make.bottom.equalTo(0)
            
        })
        
        let bar = UIBarButtonItem.init(image: UIImage(named: "nav_share_white"), style: .Done, target: self, action: #selector(self.shareToSocial))
        
        navigationItem.rightBarButtonItem = bar
        
    }
    
    //更新视图
    func updateView() {
        
        
        setCellHList()
        headview?.model = model
        tableView?.reloadData()//
        
    }
    
    func setCellHList() {
        
        
        cellHeightList = [150,190,155,300,100,260]
        
        for (index,item) in cellList.enumerate() {
            
            
            
            item.isHiddenEditBtn = false
            
            item.isHiddenEditBtn = false
            
            
            switch index {
                
            case 3:
                
                guard let cell = cellList[index] as?LDMyResumeCellMyAdvantageInAll else { return }
                
                
                cell.heightlightView?.vocieBtnClosure = {
                    [weak self] (obj) in
                    
                    let api = LDCommentAPI.getAudioApi(self?.model?.audiohighlights ?? "")
                    self?.judgeIsPlay(api)
                    self?.tapedVoiceBtn = cell.heightlightView?.voiceBtn
                }
                
                cell.strengthView?.vocieBtnClosure = {
                    [weak self] (obj) in
                    
                    let api = LDCommentAPI.getAudioApi(self?.model?.audiomystrengths ?? "")
                    self?.judgeIsPlay(api)
                    self?.tapedVoiceBtn = cell.strengthView?.voiceBtn
                }
                
                
            case 4:
                
                cellHeightList[index] = item.getCellHeight(model!)
                
                guard let cell = cellList[index] as? LDMyResumeCellExperienceInAll else { return }
                
                cell.vocieBtnClosure = {
                    [weak self] (obj) in
                    
                    let api = LDCommentAPI.getAudioApi(self?.model?.audioworkexperience ?? "")
                    
                    self?.judgeIsPlay(api)
                    self?.tapedVoiceBtn = cell.voiceBtn.voiceBtn
                }
                
            case 5:
                
                cellHeightList[index] = item.getCellHeight(model!)
                guard let cell = cellList[index] as? LDMyResumeCellEduInAll else { return }
                
                cell.vocieBtnClosure = {
                    [weak self] (obj) in
                    
                    let api = LDCommentAPI.getAudioApi(self?.model?.audioeduexperience ?? "")
                    self?.judgeIsPlay(api)
                    self?.tapedVoiceBtn = cell.voiceBtn.voiceBtn
                }
                
            default:
                break
            }
            
            switch index {
                
            case 0:
                let cell = cellList[0] as?LDMyResumeCellAvatarInAll
                cell?.headImgTitle?.text = ""//model?.name ?? ""
                cell?.headImgTitle?.userInteractionEnabled = false
            case 3,4,5:
                
                cellHeightList[index] = item.getCellHeight(model!)
                
            default:
                break
            }
            
            
            //编辑按钮
            item.tapEditBtn({ [weak self](obj) in
                
                
                switch index {
                    
                case 1://基本信息
                    let vc = DDEditMineInfoViewController()
                    vc.model = DDUserInfoModel.yy_modelWithDictionary(LDUserInfo.sharedInstance.userInfoDict)
                    self?.navigationController?.pushViewController(vc, animated: false)
                case 2://期望工作
                    let vc = LDManageMyResumeVC()
                    vc.isShowTopManger = false
                    self?.navigationController?.pushViewController(vc, animated: false)
                    break
                case 3://亮点优势
                    
                    let vc = LDEditMyResumeVC()
                    vc.editType = LDEditMyResumeVCType.HeigthLightStrength
                    self?.navigationController?.pushViewController(vc, animated: false)
                    break
                case 4://工作经历
                    let vc = LDEditMyResumeVC()
                    vc.editType = LDEditMyResumeVCType.WorkExp
                    self?.navigationController?.pushViewController(vc, animated: false)
                    break
                case 5://教育经历
                    let vc = LDEditMyResumeVC()
                    vc.editType = LDEditMyResumeVCType.EduExp
                    self?.navigationController?.pushViewController(vc, animated: false)
                    break
                default:
                    break
                }
                
                })
            
        }
        
    }
    
    //kvo
    func initKVO() {
        
        //hidden tabbar
        rac_signalForSelector(#selector(viewWillAppear)).subscribeNext { [weak self] (obj) in
            
            self?.rdv_tabBarController.setTabBarHidden(true, animated: false)
            self?.sendRequest()
        }
        
        //reflesh headview
        RACObserve(self, "model").subscribeNext { [weak self](obj) in
            
            if obj == nil { return }
            self?.updateView()
        }
        
        //更新头像
        NSNotificationCenter.defaultCenter().rac_addObserverForName(NOTI_CATTLE_EDIT_PERSIONINFO, object: nil).subscribeNext {[weak self]  (obj) in
            
            self?.sendRequest()
        }
        //
        
    }
    
    //初始化数据
    func initData() {
        
    }
    
    //请求微简历
    func sendRequest(){
        
        LDUserInfo.sharedInstance.queryJobFinderMyResume({ [weak self] (dict) in
            
            self?.model = DDMyResumeModel.yy_modelWithDictionary(dict)

        }) { (error) in
            
        }
    }
    //share
    
    func shareToSocial() {
        
        
        let resumeDict = LDUserInfo.sharedInstance.myRemuseInfoDict
        let resumeModel = DDMyResumeModel.yy_modelWithDictionary(resumeDict)
        
        let logoName = resumeModel?.sharelogo ?? ""
        let shareLogoUrl = LDCommentAPI.getPicApi(logoName)
        
        
        let shareH5 =  LDCommentAPI.getAbsoluteUrl(resumeModel?.shareh5 ?? "")
        
        //h5
        shareModel.shareH5 = shareH5
        
        //title
        shareModel.shareTitle_SMS = resumeModel?.sharetitle_sms ?? "短信"
        shareModel.shareTitle_WeiBo = resumeModel?.sharetitle_weibo ?? "微博"
        shareModel.shareTitle_WeiXin = resumeModel?.sharetitle_weixin ?? "微信"
        
        //desc
        shareModel.shareDesc = resumeModel?.sharedesc ?? "分享详情"
        
        //logo
        shareModel.shareLogoUrl = shareLogoUrl
        
        
        shareView.shareModel = shareModel
        shareView.show()
    }
    
    //判断是否播放
    func judgeIsPlay(api:String) {
        
        if audioPlayer.isPlaying == true {
            
            audioPlayer.pause()
            return
        }
        audioPlayer.setPrepareMusicUrl(api)
    }
    
}

extension LDMyResumeViewControllerInOne_Show:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return cellHeightList.count
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return cellHeightList[indexPath.row]
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let identify = NSStringFromClass(cellList[indexPath.row].classForCoder)
        
        var cell = tableView.dequeueReusableCellWithIdentifier(identify) as? LDMyResumeBaseCellInAll
        if cell == nil {
            cell = cellList[indexPath.row]
            cell?.editBtn?.removeFromSuperview()
        }
        cell?.model = model
        
        return cell!
    }
    
}

extension LDMyResumeViewControllerInOne_Show:LDAudioPlayerDelegate {
    
    
    
    func tapbtnClosure() {
        
        
        audioPlayer.playProgress = {[weak self]
            (obj,pro) in
            
            self?.tapedVoiceBtn?.animaPlay()
        }
        
        audioPlayer.playEnd = {obj in
        
        
        }
        
        
        
    }
    func audioplayerPlayWith(audioplayer: LDAudioPlayer!, progress: Float) {
        
        tapedVoiceBtn?.animaPlay()
    }
    
    func audioplayerDidFinishItem(audioplayer: LDAudioPlayer!) {
        
        audioPlayer.pause()
        tapedVoiceBtn?.animaStop()
    }
    
    func playError(audioplayer: LDAudioPlayer!) {
        audioplayer.pause()
        tapedVoiceBtn?.animaStop()
    }
}
